package bank.controler;

import java.util.Iterator;
import java.util.List;

import bank.model.BankApplicationImpl;
import bank.model.Bank;
import bank.view.ListView;

public class BankControler {
	
	private BankApplicationImpl bankApp;
	private Bank currentBank;
	private ListView view = new ListView();
	
	
	public ListView getView() {
		return view;
	}
	
	private Bank getBankById(String id) {
		int Bankid = Integer.parseInt(id);
		for (Iterator<Bank> i = getBanks().iterator(); i.hasNext();) {
			Bank b = i.next();
			if (b.getId() == Bankid)
				return b;
		}
		return null;
	}

	public List<Bank> getBanks() {
		return bankApp.getBanks();
	}

	public void setBanks(List<Bank> banks) {
		/*
		this.banks = banks;
		if (banks.size()>0)
			currentBank = banks.get(0);
			*/
	}
	
	public Bank getCurrentBank() {
		return currentBank;
	}

	public void setCurrentBank(Bank currentBank) {
		this.currentBank = currentBank;
	}	
	
	
	public void setView(ListView view) {
		//this.view = view;
	}

	public BankApplicationImpl getBankApp() {
		return bankApp;
	}

	public void setBankApp(BankApplicationImpl bankApp) {
		this.bankApp = bankApp;
	}
	

	private void newBank() {
		currentBank = new Bank("nomane",
				"noaddress", "nozip");
		getBanks().add(currentBank);
	}
	
	private void deleteBank(Bank Bank) {
		for (Iterator<Bank> i = getBanks().iterator(); i.hasNext();) {
			Bank b =  i.next();
			if (Bank == b)
				i.remove();
		}	
	}
	
	public void add(Bank Bank) {
		getBanks().add(Bank);
	}
	
	public String applyCustomer() {
		System.out.println("apply");
		return "edit_customer";
	}
	
	public String cancelBank() {
		System.out.println("cancel");
		return "list_bank";
	}
	
	public String createBank() {
		System.out.println("create");
		currentBank = null;
		newBank();
		return "edit_bank";
	}
	
	public String saveBank(){
		System.out.println("save");
		//saveBank(currentBank);
		return "list_bank";
	}
	
	public String deleteBank(){
		System.out.println("delete");		
		deleteBank(currentBank);
		currentBank = getBanks().get(0);
		return "list_bank";
	}	
	
	public String editBank() {
		System.out.println("edit");
		String BankId=view.getIdCmdValue();
		System.out.println(BankId);
		currentBank = getBankById(BankId);
		return "edit_bank";
	}
	
	public String showCustomers() {
		System.out.println("showCustomers");
		return "list_customer";
	}


	

}
